﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models.SYS.ViewModel;
using KPIS.GERP.OMVC.Classes;
using KPIS.GERP.OMVC.EmployeeService;
using KPIS.GERP.OMVC.SysApprovalService;
using KPIS.GERP.OMVC.UserService;
using System.Data.Entity;

namespace KPIS.GERP.OMVC.Controllers
{
    public class ApproveProcessController : Controller
    {
        private GERPContext context = new GERPContext(true);
        private int empId {get { return new SsMng().EmpSeqI; }}
         
        public ActionResult Index(int? appPrcGrpId = null)
        {
            ViewBag.appPrcGrpId = appPrcGrpId;
            return View();
        }
        
        public ActionResult Create(int? appPrcGrpId = null)
        {
            ViewBag.appPrcGrpId = appPrcGrpId;
            return View();
        }

        [HttpPost]
        public ActionResult Create(ApproveProcess model, TypeOfCompare conditionType, 
            int? appPrcGrpId = null, decimal mappingvalue = 0, string mappingDescription = "",
            string useMapping = "")
        {
            
            ValidateModel(model);
            if(!ModelState.IsValid){
                return View(model);
            } 

            ViewBag.appPrcGrpId = appPrcGrpId;
            model.RecordStatus = "U";
            model.CreatedBy = empId;
            model.CreatedWhen = DateTime.Now;
            model = context.ApproveProcesses.Add(model);
            model.ApproveProcessGroup = null;
            context.SaveChanges();
            
            var context2 = new GERPContext(true);
            if(useMapping == "use")
            {
                var mapping = context2.ApproveProcessMappings.ToList().Where(o => o.AppPrcId == model.AppPrcId);
                ApproveProcessMapping currentMapping = new ApproveProcessMapping();
                if (mapping.Any())
                {
                    foreach (var m in mapping)
                    {
                        if (m != mapping.First())
                        {
                            context.ApproveProcessMappings.Remove(m);
                        }
                        else
                        {
                            currentMapping = m;
                        }
                    }
                }
                else
                {
                    context2.ApproveProcessMappings.Add(currentMapping);
                }
                currentMapping.AppPrcGrpId = model.AppPrcGrpId;
                currentMapping.AppPrcId = model.AppPrcId;
                currentMapping.Description = mappingDescription;
                currentMapping.TypeOfCompare = conditionType;
                currentMapping.ValueToCompare = mappingvalue;
                context2.SaveChanges();
            } else
            {
                var currentMappings = context2.ApproveProcessMappings.Where(o => o.AppPrcId == model.AppPrcId);
                foreach(var cm in currentMappings)
                {
                    context2.ApproveProcessMappings.Remove(cm);
                }
                context2.SaveChanges();
            }
            return RedirectToAction("Detail", new {id = model.AppPrcId, appPrcGrpId});
        }
        
        public ActionResult Update(int id, int? appPrcGrpId = null)
        {
            return View(context.ApproveProcesses.FirstOrDefault(o=>o.AppPrcId == id));
        }

        [HttpPost]
        public ActionResult Update(ApproveProcess model, TypeOfCompare conditionType, decimal mappingvalue = 0, string mappingDescription = "",
            string useMapping = "")
        {
            ValidateModel(model);
            if(!ModelState.IsValid){
                return View(model);
            } 
            context.ApproveProcesses.Attach(model);
            context.Entry(model).State = EntityState.Modified;
            model.UpdatedBy = empId;
            model.UpdatedWhen = DateTime.Now;
            context.SaveChanges();
            
            var context2 = new GERPContext(true);
            if(useMapping == "use")
            {
                var mapping = context2.ApproveProcessMappings.ToList().Where(o => o.AppPrcId == model.AppPrcId);
                ApproveProcessMapping currentMapping = new ApproveProcessMapping();
                if (mapping.Any())
                {
                    foreach (var m in mapping)
                    {
                        if (m != mapping.First())
                        {
                            context.ApproveProcessMappings.Remove(m);
                        }
                        else
                        {
                            currentMapping = m;
                        }
                    }
                }
                else
                {
                    context2.ApproveProcessMappings.Add(currentMapping);
                }
                currentMapping.AppPrcGrpId = model.AppPrcGrpId;
                currentMapping.AppPrcId = model.AppPrcId;
                currentMapping.Description = mappingDescription;
                currentMapping.TypeOfCompare = conditionType;
                currentMapping.ValueToCompare = mappingvalue;
                context2.SaveChanges();
            } else
            {
                var currentMappings = context2.ApproveProcessMappings.Where(o => o.AppPrcId == model.AppPrcId);
                foreach(var cm in currentMappings)
                {
                    context2.ApproveProcessMappings.Remove(cm);
                }
                context2.SaveChanges();
            }

            return RedirectToAction("Detail", new {id = model.AppPrcId, appPrcGrpId = model.AppPrcGrpId});
        }
        
        public ActionResult Detail(int id, int? appPrcGrpId = null, int? addTo = null)
        {
            ViewBag.appPrcGrpId = appPrcGrpId;
            ViewBag.addTo = addTo;
            return View(context.ApproveProcesses.Include(o=>o.ApproveProcessGroup).FirstOrDefault(o=>o.AppPrcId == id));
        }
        
        public ActionResult Delete(int id, int? appPrcGrpId = null)
        {
            ViewBag.appPrcGrpId = appPrcGrpId;
            context.ApproveProcesses.FirstOrDefault(o => o.AppPrcId == id).RecordStatus = "D";
            context.SaveChanges();
            return RedirectToAction("Index", new {appPrcGrpId});
        }

        public ActionResult Enable(int id, int? appPrcGrpId = null)
        {
            ViewBag.appPrcGrpId = appPrcGrpId;
            context.ApproveProcesses.FirstOrDefault(o => o.AppPrcId == id).RecordStatus = "U";
            context.SaveChanges();
            return RedirectToAction("Index", new {appPrcGrpId});
        }
        public void ValidateModel(ApproveProcess model)
        {
            if(string.IsNullOrWhiteSpace(model.Name))
            {
                ModelState.AddModelError("Name", "กรุณาใส่ชื่อ");
            }
        }
    }
}
